class Solution {
public:
    vector<bool> vis;

    int findCircleNum(vector<vector<int>>& graph) {
        int n=graph.size();
        vis.resize(n, false);
        int cnt=0;
        for(int st=0; st<n; st++)
            if(!vis[st]) {
                cnt++;
                queue<int> q;
                q.push(st);
                while(!q.empty())
                {
                    auto u=q.front(); q.pop();
                    vis[u]=1;
                    for(int v=0; v<n; v++)
                        if(!vis[v]&&graph[u][v]) q.push(v);
                }
            }
        return cnt;
    }
};